package com.eastseeway.radioproject.utils;

import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.eastseeway.radioproject.R;
import com.eastseeway.radioproject.RadioApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

public class SaveHelper {

    public static String BaseStoragePath = Environment.getExternalStorageDirectory().getPath() + File.separator + RadioApplication.getAppContext().getString(R.string.app_name);

//    public static void checkBaseStoragePath() {
//        if (BaseStoragePath == null) {
//            BaseStoragePath = Environment.getExternalStorageDirectory().getPath() + File.separator + RadioApplication.getAppContext().getString(R.string.app_name);
//        }
//    }

    //    获取Download文件夹目录
    public static String getDownloadParentPath() {
        return Environment.getExternalStorageDirectory().getPath() + File.separator + "Download";
    }

    public static String getLogFilePath() {
        String parentPath = createLogDirectory();
        return parentPath + File.separator + TimeFormatter.format_yyyyMMdd(new Date()) + ".txt";
    }

    public static String createLogDirectory() {
//        checkBaseStoragePath();
        String logDirectoryName = BaseStoragePath + File.separator + "log";
        File folder = new File(logDirectoryName);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return logDirectoryName;
    }

    //   获取操作参数文件保存目录
    public static String createOperationParamsDirectory() {
        String operationParamsParentPath = BaseStoragePath + File.separator + "operation_params";
        File folder = new File(operationParamsParentPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return operationParamsParentPath;
    }

    public static String getVBusFileName() {
        return SaveHelper.BaseStoragePath + File.separator + "VBus.json";
    }

    public static String getOperationParamsFilePath(String username) {
        return createOperationParamsDirectory() + File.separator + username + ".txt";
    }

    public static Uri getSaveFileUri() {
        return Uri.fromFile(new File(getLogFilePath()));
    }

    public static String getSaveFirmwarePath(String fileName) {
        String firmwareParentPath = getFirmwareParentPath();
        return firmwareParentPath + File.separator + fileName;
    }

    public static String getFirmwareParentPath() {
//        checkBaseStoragePath();
        String parentPath = BaseStoragePath + File.separator + "firmware";
        File folder = new File(parentPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return parentPath;
    }

    public static String getSaveVideoPath() {
//        checkBaseStoragePath();

        String parentPath = BaseStoragePath + File.separator + TimeFormatter.format_yyyyMMdd(new Date()) + File.separator + "video";
        File folder = new File(parentPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return parentPath + File.separator + TimeFormatter.format_yyyyMMdd(new Date()) + ".mp4";
    }

    public static Uri getSaveVideoUri() {
        return Uri.fromFile(new File(getSaveVideoPath()));
    }

    /**
     * 定义文件保存的方法，写入到文件中，
     */
    // 在 SaveHelper.java 中修改 write2File 方法
    public static boolean write2File(String filePath, String content) {
        FileOutputStream fos = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            fos.write(content.getBytes());
            fos.flush();
            return true; // 操作成功返回 true
        } catch (IOException e) {
            e.printStackTrace();
            return false; // 操作失败返回 false
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 在 SaveHelper.java 中添加以下方法
public static String readFromFile(String filePath) {
    StringBuilder contentBuilder = new StringBuilder();
    BufferedReader reader = null;
    try {
        File file = new File(filePath);
        if (!file.exists()) {
            return null; // 文件不存在返回null
        }

        reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            contentBuilder.append(line);
        }
        return contentBuilder.toString(); // 返回文件内容
    } catch (IOException e) {
        e.printStackTrace();
        return null; // 读取失败返回null
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



}
